package xyz.pro5books.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.pro5books.pojo.Book;
import xyz.pro5books.pojo.User;
import xyz.pro5books.service.BookService;
import xyz.pro5books.service.UserService;
import xyz.pro5books.vo.PageResult;
import xyz.pro5books.vo.SysResult;
import xyz.pro5books.vo.UserInfoToken;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private BookService bookService;

    //用户查询
    @GetMapping("/list")
    public SysResult getUser(PageResult pageResult) {
        pageResult = userService.getUser(pageResult);
        //Object a = pageResult.getRows();

        return SysResult.success(pageResult);
    }

    //下载权限修改
    @PutMapping("/downRight/{id}/{downRight}")
    public SysResult updateDownRight(User user) {

        userService.updateDownRight(user);
        return SysResult.success();
    }

    //上传权限修改
    @PutMapping("/upRight/{id}/{upRight}")
    public SysResult updateUpRight(User user) {

        userService.updateUpRight(user);
        return SysResult.success();
    }

    //用户信息修改
    //1.先查询要修改的信息并显示在修改窗口
    @GetMapping("{id}")
    public SysResult getUserById(@PathVariable Integer id) {
        User user = userService.getUserById(id);
        return SysResult.success(user);

    }

    //2.修改用户信息
    @PutMapping("/updateUser")
    public SysResult updateUser(@RequestBody User user) {
        userService.updateUser(user);
        return SysResult.success();
    }

    @PutMapping("/addBookForm")
    public SysResult addBookForm(@RequestBody Book book){
        bookService.addBookForm(book);
        return SysResult.success();
    }

    @PutMapping("/updatePhoneById")
    public SysResult updatePhoneById(@RequestBody User user){
        userService.updatePhoneById(user);
        return SysResult.success();
    }

    @PutMapping("/updatePwdById")
    public SysResult updatePwdById(@RequestBody User user){
        userService.updatePwdById(user);
        return SysResult.success();
    }


    //用户信息删除
    @DeleteMapping("/{id}")
    public SysResult deleteUser(@PathVariable Integer id) {
        userService.deleteUser(id);
        return SysResult.success();
    }

    //用户登录
    @PostMapping("/login")
    public SysResult login(@RequestBody User user) {
        UserInfoToken userInfoToken = userService.login(user);
        if (userInfoToken.getId()!=0) {
            return SysResult.success(userInfoToken);
        } else {
            return SysResult.fail();
        }
    }

    @PutMapping("/signUp")
    public SysResult signUp(@RequestBody User user){
        userService.signUp(user);
        return SysResult.success();
    }

    //查询用户注册时间
    @GetMapping("/getUserCreateList")
    public SysResult getUserCreateList(){
        List list = userService.getUserCreate();
        return SysResult.success(list);
    }
    //用户注册数量统计
    @GetMapping("/userCount")
    public SysResult selUserCount(){
        List list = userService.selUserCount();
        return SysResult.success(list);
    }

    //用户信息获取
    @GetMapping("/getInfo/{userId}")
    public SysResult getInfo(@PathVariable Integer userId){
        User user = userService.getInfo(userId);
        System.out.println(user.toString());
        System.out.println(userId);
        return SysResult.success(user);
    }

    @GetMapping("/getUserTokenInfo/{userId}")
    public SysResult getUserTokenInfo(@PathVariable Integer userId){
        UserInfoToken userInfoToken = userService.getUserTokenInfo(userId);
        if (userInfoToken != null){
            return SysResult.success(userInfoToken);
        }else {
            return SysResult.fail();
        }
    }

    //用户信息修改
    @PutMapping("/user/updateUserInfo")
    public SysResult updateUserInfo(User user){
        userService.updateUserInfo(user);
        return SysResult.success();
    }

    @GetMapping("/getVerifyCode/{phoneNumber}")
    public SysResult getVerifyCode(@PathVariable String phoneNumber){
        String verifyCode = userService.getVerifyCode(phoneNumber);
        if (verifyCode.length() == 6){
            return SysResult.success(verifyCode);
        }else {
            return SysResult.fail();
        }
    }

    @PutMapping("/updateUsernameById/{userId}/{username}")
    public SysResult updateUsernameById(@PathVariable Integer userId,@PathVariable String username){
        userService.updateUsernameById(userId,username);
        return SysResult.success();
    }

    @PutMapping("/updateEmailById/{userId}/{email}")
    public SysResult updateEmailById(@PathVariable Integer userId,@PathVariable String email){
        userService.updateEmailById(userId,email);
        return SysResult.success();
    }

    @GetMapping("/getUserByName/{userName}")
    public SysResult getUserByName(@PathVariable String userName){
        return SysResult.success(userService.getUserByName(userName));
    }

}
